﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZEFIT
{
    public partial class NewCustomerForm : Form
    {
        Controller controller = null;
        Customer dataObject = null;
        CustomersForm cForm = null;

        public NewCustomerForm(CustomersForm cForm)
        {
            this.cForm = cForm;
            InitializeComponent();
            controller = new Controller(new MYSQLAdabterCustomer());
            dataObject = new Customer();
        }

        public NewCustomerForm(CustomersForm cForm, Customer dataObject)
        {
            this.cForm = cForm;
            InitializeComponent();
            controller = new Controller(new MYSQLAdabterCustomer());
            this.dataObject = dataObject;
            txt_name.Text = dataObject.Name;
            txt_zip.Text = dataObject.ZIP.ToString();
            txt_country.Text = dataObject.Adress;
            txt_address.Text = dataObject.Country;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            Boolean failed = false;
            if(txt_name.Text == ""){
                lbl_name_error.Visible = true;
                failed = true;
            }
            if (txt_address.Text == "")
            {
                lbl_address_error.Visible = true;
                failed = true;
            }
            if (txt_country.Text == "")
            {
                lbl_country_error.Visible = true;
                failed = true;
            }
            if (txt_zip.Text == "")
            {
                lbl_zip_error.Visible = true;
                failed = true; return;
            }
            if (failed) { return; }
            dataObject.Name = txt_name.Text;
            dataObject.ZIP = Convert.ToInt32(txt_zip.Text);
            dataObject.Adress = txt_country.Text;
            dataObject.Country = txt_address.Text;
            controller.Save(dataObject);
            cForm.dataLoad();
            this.Close();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            cForm.dataLoad();
            this.Close();
        }
    }
}
